using Bookvar.ApplicationModel;
using Bookvar.ObjectModel;
using Bookvar.ObjectModel.Actions;
using Bookvar.ServiceModel;
using Bookvar.ServiceModel.Actions;

namespace Bookvar.UI
{
    public static class TopicActions
    {
        public static IAction CreateAddAttachmentAction( Attachment attachment, string fileName, Topic attachmentTopic,
                                                          MindMap mindmap )
        {
            var persistenceService = BookvarApplication.Current.ServiceLayer.GetService<IPersistenceService>();

            var compositeAction = new CompositeAction();

            var addAttachmentAction = new ChangeTopicAction( attachmentTopic );
            addAttachmentAction.AttachmentsToAdd.Add( attachment );
            compositeAction.Add( addAttachmentAction );

            compositeAction.Add( new SaveFileResourceAction( persistenceService, mindmap, attachment, fileName ) );

            return compositeAction;
        }

        public static IAction CreateDeleteAttachmentAction(Attachment attachment, Topic attachmentTopic, MindMap mindmap)
        {
            var persistenceService = BookvarApplication.Current.ServiceLayer.GetService<IPersistenceService>();

            var compositeAction = new CompositeAction();

            var deleteAttachmentAction = new ChangeTopicAction( attachmentTopic );
            deleteAttachmentAction.AttachmentsToRemove.Add( attachment );
            compositeAction.Add( deleteAttachmentAction );

            compositeAction.Add( new DeleteFileResourceAction( persistenceService, mindmap, attachment ) );

            return compositeAction;
        }

        public static IAction CreateAddResourceAction(Resource resource, string fileName, Topic resourceTopic, MindMap mindmap)
        {
            var persistenceService = BookvarApplication.Current.ServiceLayer.GetService<IPersistenceService>();

            var compositeAction = new CompositeAction();

            var addResourceAction = new ChangeTopicAction( resourceTopic );
            addResourceAction.ResourcesToAdd.Add( resource );
            compositeAction.Add( addResourceAction );

            compositeAction.Add( new SaveFileResourceAction( persistenceService, mindmap, resource, fileName ) );

            return compositeAction;
        }

        public static IAction CreateDeleteResourceAction( Resource resource, Topic resourceTopic, MindMap mindmap )
        {
            var persistenceService = BookvarApplication.Current.ServiceLayer.GetService<IPersistenceService>();

            var compositeAction = new CompositeAction();

            var deleteResourceAction = new ChangeTopicAction( resourceTopic );
            deleteResourceAction.ResourcesToRemove.Add( resource );
            compositeAction.Add( deleteResourceAction );

            compositeAction.Add( new DeleteFileResourceAction( persistenceService, mindmap, resource ) );

            return compositeAction;
        }

        public static IAction CreateChangeTopicParentAction( Topic topic, Topic newParent, MindMap mindmap )
        {
            var action = new CompositeAction();
            action.Add( new ChangeTopicAction( topic ) { Parent = newParent } );

            var position = mindmap.TopicPositions[ topic.ID ].Clone();
            position.IsMoved = false;
            action.Add( new ChangeTopicPositionAction( topic, position, mindmap ) );

            return action;
        }
    }
}